14. Cropping Images in Keras
10 - Image Cropping
The cameras in the simulator capture 160 pixel by 320 pixel images.
Not all of these pixels contain useful information, however. In the image above, the top portion of the image captures trees and hills and sky, and the bottom portion of the image captures the hood of the car.
Your model might train faster if you crop each image to focus on only the portion of the image that is useful for predicting a steering angle.
Cropping2D Layer
Keras provides the Cropping2D layer for image cropping within the model. This is relatively fast, because the model is parallelized on the GPU, so many images are cropped simultaneously.
By contrast, image cropping outside the model on the CPU is relatively slow.
Also, by adding the cropping layer, the model will automatically crop the input images when making predictions in
drive.py
.
The Cropping2D layer might be useful for choosing an area of interest that excludes the sky and/or the hood of the car.
Here is an example of an input image and its cropped version after passing through a Cropping2D layer:
Cropping Layer Code Example
from keras.models import Sequential, Model
from keras.layers import Cropping2D
import cv2
# set up cropping2D layer
model = Sequential()
model.add(Cropping2D(cropping=((50,20), (0,0)), input_shape=(160,320,3)))
...
The example above crops:
- 50 rows pixels from the top of the image
- 20 rows pixels from the bottom of the image
- 0 columns of pixels from the left of the image
- 0 columns of pixels from the right of the image